/*
 * Copyright (c) 2009 Frederic Daoud
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.stripesbook.rayures.util;

import static org.stripesbook.rayures.util.Constants.CONFIG_LOOKUP_STRATEGY;
import static org.stripesbook.rayures.util.Constants.CONFIG_RESOURCE_BUNDLE_RESOLVER;
import static org.stripesbook.rayures.util.Constants.CONFIG_TEMPLATE_ENGINE;
import static org.stripesbook.rayures.util.Constants.CONFIG_VIEW_PREPARER;
import java.util.HashMap;
import java.util.Map;
import net.sourceforge.stripes.config.ConfigurableComponent;
import net.sourceforge.stripes.config.Configuration;
import org.stripesbook.rayures.service.LookupStrategy;
import org.stripesbook.rayures.service.ResourceBundleResolver;
import org.stripesbook.rayures.service.TemplateEngine;
import org.stripesbook.rayures.service.ViewPreparer;
import org.stripesbook.rayures.service.impl.LookupStrategyImpl;
import org.stripesbook.rayures.service.impl.ResourceBundleResolverImpl;
import org.stripesbook.rayures.service.impl.ViewPreparerImpl;
import org.stripesbook.rayures.view.velocity.VelocityTemplateEngine;

public final class Resolver {
  private static class Config {
    public String initParam;
    public Class<?> defaultImpl;

    public Config(String initParam, Class<?> defaultImpl) {
      this.initParam = initParam;
      this.defaultImpl = defaultImpl;
    }
  }

  private static Map<Class<?>, Config> map = new HashMap<Class<?>, Config>();

  static {
    map.put(LookupStrategy.class,
      new Config(CONFIG_LOOKUP_STRATEGY, LookupStrategyImpl.class));
    map.put(ResourceBundleResolver.class,
      new Config(CONFIG_RESOURCE_BUNDLE_RESOLVER, ResourceBundleResolverImpl.class));
    map.put(TemplateEngine.class,
      new Config(CONFIG_TEMPLATE_ENGINE, VelocityTemplateEngine.class));
    map.put(ViewPreparer.class,
      new Config(CONFIG_VIEW_PREPARER, ViewPreparerImpl.class));
  }

  @SuppressWarnings("unchecked")
  public static <T> T resolve(Configuration config, Class<T> type) throws Exception {
    Config cfg = map.get(type);

    if (cfg != null) {
      Class<? extends T> defaultImpl = (Class<? extends T>) cfg.defaultImpl;
      return resolve(config, type, cfg.initParam, defaultImpl);
    }
    return null;
  }

  public static <T> T resolve(Configuration config, Class<T> type, String initParam,
    Class<? extends T> defaultImpl)
    throws Exception
  {
    T resolved = null;

    Class<? extends T> resolvedClass =
      config.getBootstrapPropertyResolver().getClassProperty(initParam, type);

    resolved = config.getObjectFactory().newInstance(
      resolvedClass != null ? resolvedClass : defaultImpl);

    if (resolved instanceof ConfigurableComponent) {
      ((ConfigurableComponent) resolved).init(config);
    }

    return resolved;
  }
}
